﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;

using Jungle.Code.BookManager;
using Jungle.Code.News;
using Jungle.Modules;
using System.Drawing;
using Jungle.Code.Core;

namespace Jungle.Modules
{
    public partial class News_User : BaseUserModule
    {
        string cmd = "";
        string nId = "";
        string catId = "";
        string largeImg = "";


        protected override void OnInit(EventArgs e)
        {

            if (Request["cmd"] != null)
                cmd = Request["cmd"];
            if (Request["nId"] != null)
                nId = Request["nId"];
            if (Request["catId"] != null)
                catId = Request["catId"];
            if (Request["large"] != null)
                largeImg = Request["large"];


            switch (cmd)
            {
                case "view":
                    News_List.ActiveViewIndex = 0;
                    break;
                case "search":
                    News_List.ActiveViewIndex = 0;
                    break;
                case "details":
                    if (largeImg == "1") 
                    {
                        News_List.ActiveViewIndex = 2;
                        PrepareLargeImg();
                    }
                    else
                    {
                        News_List.ActiveViewIndex = 1;
                        PrepareView();
                    }
                    break;
                default:
                    News_List.ActiveViewIndex = 0;
                    break;
            }
            


            base.OnInit(e);
        }

        public void PrepareView()
        {
            News n = new News();

            n.IdNews = Convert.ToInt32(nId);
            n.GetById();

            lbl_Body.Text = n.Body.Trim();
            lbl_PubDate.Text = n.PubDate.ToLongDateString();
            lbl_Title.Text = n.Title.Trim();
            if (n.Img != "no_photo")
            {
                img_Image.ImageUrl = n.Img;
            }
            else 
            {
                img_Image.ImageUrl = "/UserFiles/Images/Content/no_photo.jpg";
            }
        }
        //-------------------------------------------------------
        public void PrepareLargeImg()
        {
            News p = new News();
            p.IdNews = Convert.ToInt32(nId);
            p.GetById();

            if (p.Img.ToString().Trim() != "no_photo")
            {
                string[] ImagesUrls = p.Img.ToString().Split(',');

                try
                {
                    img_Large.ImageUrl = UrlHelper.GetSiteUrl() + ImagesUrls[1].Trim();
                }
                catch (Exception ee)
                {
                    img_Large.ImageUrl = UrlHelper.GetSiteUrl() + "/UserFiles/Images/Content/no_photo.jpg";
                }

            }
            else
            {
                img_Large.ImageUrl = UrlHelper.GetSiteUrl() + "/UserFiles/Images/Content/no_photo.jpg";
            }
        }

    }
}